<?php
namespace app\source\controller\card;

use app\common\controller\Api;

/**
 * 通讯录接口
 */
class Card extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
        
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business');
        $this->CardModel = model('Card.Card');
        $this->TypeModel = model('Card.Type');
        $this->RegionModel = model('Region');
    }

    /**
     * 查询通讯录
     *
     * @ApiTitle    (查询通讯录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/index)
     * @ApiParams   (name="page", type="string", required=true, description="当前页数")
     * @ApiParams   (name="limit", type="string", required=true, description="每页显示多少条数据")
     * @ApiParams   (name="typeid", type="string", required=true, description="分类ID")
     * @ApiParams   (name="search", type="string", required=true, description="搜索关键词")
     * @ApiParams   (name="businessid", type="string", required=true, description="用户ID")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "查询通讯录",
            "time": "时间戳",
            "data": {
                "list": [
                    {
                        "id": 1,
                        "nickname": "张三", //昵称
                        "mobile": "13512644557", //手机号
                        "gender": "0", //性别
                        "province": "440000", //省份
                        "city": "440100", //城市
                        "district": "440105", //区域
                        "remark": "123", //备注
                        "createtime": 1683712065, //添加时间
                        "typeid": 12, //分类id
                        "gender_text": "保密", //性别中文
                        "region_text": "广东省-广州市-海珠区", //地区中文
                        "province_text": "广东省", //省份中文
                        "city_text": "广州市",  //城市中文
                        "district_text": "海珠区" //地区中文
                    },
                ],
                "count": 40, //总条数
        })
     */
    public function index()
    {
        if($this->request->isPost())
        {
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');
            $typeid = $this->request->param('typeid', 0, 'trim');
            $businessid = $this->request->param('businessid', 0, 'trim');
            $search = $this->request->param('search', '', 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            $where = [];

            //判断是否有选择分类
            if($typeid != 0)
            {
                $where['typeid'] = $typeid;
            }

            if(!empty($search))
            {
                $where['nickname'] = ['like', "%$search%"];
            }

            //总条数
            $count = $this->CardModel->where($where)->count();

            // 偏移量
            $start = ($page-1)*$limit;

            $list = $this->CardModel->where($where)->order('createtime desc, id desc')->limit($start, $limit)->select();

            if($list)
            {
                $data = [
                    'list'=>$list,
                    'count'=>$count
                ];
                $this->success('返回数据成功', $data);
                exit;
            }else
            {
                $this->error('暂无更多数据');
                exit;
            }
        }
    }

    /**
     * 添加通讯录
     *
     * @ApiTitle    (添加通讯录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/add)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="nickname", type="string", required=true, description="昵称")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号码")
     * @ApiParams   (name="gender", type="string", required=true, description="性别")
     * @ApiParams   (name="typeid", type="string", required=true, description="分类ID")
     * @ApiParams   (name="region", type="string", required=false, description="地区码")
     * @ApiParams   (name="remark", type="string", required=false, description="备注信息")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "添加通讯录成功",
            "time": "时间戳",
            "data": null
        })
     */
    public function add()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');

            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('会员信息不存在');
                exit;
            }

            //组装数据
            $data = [
                'nickname' => $this->request->param('nickname', '', 'trim'),
                'mobile' => $this->request->param('mobile', '', 'trim'),
                'gender' => $this->request->param('gender', '', 'trim'),
                'remark' => $this->request->param('remark', '', 'trim'),
                'typeid' => $this->request->param('typeid', NULL, 'trim'),
            ];

            //判断是否有选择城市信息
            $region = $this->request->param('region', '', 'trim');

            if(!empty($region))
            {
                $path = $this->RegionModel->where(['code' => $region])->value('parentpath');

                if(!empty($path))
                {
                    $list = explode(',', $path);
                    
                    if(isset($list[0]))
                    {
                        $data['province'] = $list[0];
                    }

                    if(isset($list[1]))
                    {
                        $data['city'] = $list[1];
                    }

                    if(isset($list[2]))
                    {
                        $data['district'] = $list[2];
                    }
                }
            }
            
            $result = $this->CardModel->validate('common/Card/Card')->save($data);

            if($result === FALSE)
            {
                $this->error($this->CardModel->getError());
                exit;
            }else
            {
                $this->success('添加通讯录成功');
                exit;
            }
        }
    }

    /**
     * 删除通讯录
     *
     * @ApiTitle    (删除通讯录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/del)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="cardid", type="string", required=true, description="删除的通讯录ID")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "删除通讯录成功",
            "time": "时间戳",
            "data": null
        })
     */
    public function del()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');
            $cardid = $this->request->param('cardid', 0, 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            //判断删除的通讯录是否存在
            $card = $this->CardModel->where(['id' => $cardid])->find();

            if(!$card)
            {
                $this->error('要删除的通讯录不存在');
                exit;
            }
            
            $result = $this->CardModel->destroy($cardid);

            if($result === FALSE)
            {
                $this->error($this->CardModel->getError());
                exit;
            }else
            {
                $this->success('删除通讯录成功');
                exit;
            }
        }
    }

    /**
     * 编辑通讯录
     *
     * @ApiTitle    (编辑通讯录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/edit)
     * @ApiParams   (name="cardid", type="string", required=true, description="通讯录ID")
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="nickname", type="string", required=true, description="昵称")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号码")
     * @ApiParams   (name="gender", type="string", required=true, description="性别")
     * @ApiParams   (name="typeid", type="string", required=true, description="分类ID")
     * @ApiParams   (name="region", type="string", required=false, description="地区码")
     * @ApiParams   (name="remark", type="string", required=false, description="备注信息")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "编辑通讯录成功",
            "time": "时间戳",
            "data": null
        })
     */
    public function edit()
    {
        if($this->request->isPost())
        {
            $cardid = $this->request->param('cardid', 0, 'trim');
            $businessid = $this->request->param('businessid', 0, 'trim');

            //判断通讯录是否存在
            $card = $this->CardModel->where(['id' => $cardid])->find();
            
            if(!$card)
            {
                $this->error('通讯录信息不存在');
                exit;
            }

            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('会员信息不存在');
                exit;
            }

            //组装数据
            $data = [
                'id' => $cardid,
                'nickname' => $this->request->param('nickname', '', 'trim'),
                'mobile' => $this->request->param('mobile', '', 'trim'),
                'gender' => $this->request->param('gender', '', 'trim'),
                'remark' => $this->request->param('remark', '', 'trim'),
                'typeid' => $this->request->param('typeid', NULL, 'trim'),
            ];

            //判断是否有选择城市信息
            $region = $this->request->param('region', '', 'trim');

            if(!empty($region))
            {
                $path = $this->RegionModel->where(['code' => $region])->value('parentpath');

                if(!empty($path))
                {
                    $list = explode(',', $path);
                    
                    if(isset($list[0]))
                    {
                        $data['province'] = $list[0];
                    }

                    if(isset($list[1]))
                    {
                        $data['city'] = $list[1];
                    }

                    if(isset($list[2]))
                    {
                        $data['district'] = $list[2];
                    }
                }
            }
            
            $result = $this->CardModel->validate('common/Card/Card')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->CardModel->getError());
                exit;
            }else
            {
                $this->success('更新通讯录成功');
                exit;
            }
        }
    }

    /**
     * 检查通讯录是否存在
     *
     * @ApiTitle    (检查通讯录是否存在)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/check)
     * @ApiParams   (name="businessid", type="string", required=true, description="会员ID")
     * @ApiParams   (name="cardid", type="string", required=true, description="通讯录ID")
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "查询通讯录成功",
            "time": "时间戳",
            "data": {
                "id": 1,
                "nickname": "张三", //昵称
                "mobile": "13512644557", //手机号
                "gender": "0", //性别
                "province": "440000", //省份
                "city": "440100", //城市
                "district": "440105", //区域
                "remark": "123", //备注
                "createtime": 1683712065, //添加时间
                "typeid": 12, //分类id
                "gender_text": "保密", //性别中文
                "region_text": "广东省-广州市-海珠区", //地区中文
                "province_text": "广东省", //省份中文
                "city_text": "广州市",  //城市中文
                "district_text": "海珠区" //地区中文
            }
        })
     */
    public function check()
    {
        if($this->request->isPost())
        {
            $businessid = $this->request->param('businessid', 0, 'trim');
            $cardid = $this->request->param('cardid', 0, 'trim');

            //根据ID条件查找用户
            $business = $this->BusinessModel->where(['id' => $businessid])->find();
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            //判断删除的通讯录是否存在
            $card = $this->CardModel->where(['id' => $cardid])->find();
            
            if($card)
            {
                $this->success('查询通讯录成功', $card);
                exit;
            }else
            {
                $this->error('通讯录不存在');
                exit;
            }
        }
    }



    /**
     * 获取增加通讯录下拉分类别表
     *
     * @ApiTitle    (获取增加通讯录下拉分类别表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/source/card/card/type)
     * @ApiReturnParams   (name="code", type="integer", sample="0", description="状态码")
     * @ApiReturnParams   (name="msg", type="string", sample="返回成功", description="返回信息")
     * @ApiReturnParams   (name="data", type="object", sample="null", description="返回数据")
     * @ApiReturn   ({
            "code": 1,
            "msg": "获取数据成功",
            "time": "时间戳",
            "data": [
                {id:1,name:'医院'}
            ]
        })
     */
    public function type()
    {
        if ($this->request->isPost()) 
        {
            $list = $this->TypeModel->select();
            $this->success('返回分类数据成功', $list);
            exit;
        }
    }

    
}